<?

/* TranslationHelper version alpha1 
 * Written by:	Kleist aka Andreas Kleist Svendsen
 * Created:		21st January 2008
 * Known issues:	Doesn't do anything yet
 *
 * TODO:
 * - a lot...
 * - move config to module-config
 * - implement as class
 * - Fix in template header
*/

/* Initialize TranslationHelper object */
$TH = new TranslationHelper();

return $TH->output;

class TranslationHelper
{
	/* Placeholders */
	var $ph = array(
		'message' => '',
		'content' => '',
		'fileselect' => '',
		'lang' => '',
		'baselang' => 'english'
	);
	
	var $output = '';

	/* Default config */
	var $config = array(
		'filenames' => array(
			'Manager' => 'manager/includes/lang/[+lang+].inc.php', 
			'ManagerCountries' => 'manager/includes/lang/country/[+lang+]_country.inc.php'
		),
	);
	
	function DefaultTemplate() {
		$tpl = <<<EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  lang="da" xml:lang="da">
<head>
<title>TranslationHelper - MODx module</title>
<script language="JavaScript" type="text/javascript">
function postForm(action){
document.module.action.value=action;
document.module.submit();
}
</script>
</head>
<body>
<form name="module" method="post">
<input name="action" type="hidden" value="" />
<h1>TranslationHelper</h1>
Select action: [<a href="javascript:;" onclick="postForm('save')">Save</a>][+fileselect+]
[<a href="javascript:;" onclick="postForm('load')">Load file</a>]
[<a href="javascript:;" onclick="postForm('reset')">Reset</a>]
[<a href="javascript:;" onclick="postForm('showmodxobject')">Show MODx Object</a>]
<h3>[+message+]&nbsp;</h3>
<h4>Base language: [+baselang+]</h4>
<h4>Language: [+lang+]</h4>
[+content+]
</form>
</body>
</html>
EOT;
		return $tpl;
	}	
	

	function TranslationHelper() {
		global $modx;
		/* Module logic */
		$action = isset($_POST['action']) ? $_POST['action'] : '';
		switch ($action) { // Module logic switch
			case 'save':
				$this->ph['message'] = 'Action save selected';
				break;
				
			case 'load':
				if (isset($_POST['file']))
				{
					$this->ph['message'] = 'Load file action selected. Filename: '
						.$this->config['filenames'][$_POST['file']];
				}
				else {
					$this->ph['message'] = 'Unknown file'.$_POST['file'];
				}
				break;
				
			case 'setlanguage':
				if (isset($_POST['baselang'])) {
					/* Check input */
					if ($_POST['baselang'] != basename($_POST['baselang']))
					{
						$this->ph['message'] .= "Base language should only be the name of the language, ie. 'english'";
					}
					else {
						$this->ph['baselang'] = $_POST['baselang'];
						$this->ph['message'] = "Base language set to '".$_POST['baselang']."'";
					}
						
						
					$this->ph['baselang'] = basename($_POST['baselang']);
				}
			case 'showmodxobject':
				echo "<pre>";
				print_r($modx);
				echo "</pre>";
				return;
				break;
				
			case 'reset':
			default:
				$this->ph['message'] = '';
			break;
		}
		
		/* Set languages */
		$this->ph['lang'] = $modx->config['manager_language'];

		/* Create file-select-box */
		$this->ph['fileselect'] = $this->CreateSelectBox($this->config['filenames'],'filename');

		/* Load default template */
		$this->output .= $this->DefaultTemplate();
		
		/* Replace placeholders */
		$this->ReplacePlaceholders();
	}
	

	function CreateSelectBox($values,$name) {
		$html = '';
		$html .= '<select name="'.$name."\">\n";
		foreach ($values as $n => $v) {
			$html .= '<option value="'.$v.'">'.$n."</option>\n";
		}
		$html .= "</select>\n";
		return $html;
	}

	function ReplacePlaceholders() {
		foreach($this->ph as $n=>$v) {
			$this->output = str_replace('[+'.$n.'+]',$v,$this->output);
		}
	}
}

